/**
 * 针对时间日期的转化处理
 * @param val
 * @param type
 * @param config
 * @returns {*}
 */
import moment from 'moment';
const dateTimeFormat = (val, type, config) => {
  if ( !val ) return;
  if ( type === 'date' ) {
    if ( config === 0 ) {
      return moment(val).format('YYYY-MM-DD')
    }
    if ( config === 1 ) {
      return moment(val).format('YYYY-MM-DD HH:mm:ss')
    }
    if ( config === 2 ) {
      return moment(val).format('MM-DD')
    }
    if ( config === 3 ) {
      return moment(val).format('YYYY-MM')
    }
    if ( config === 4 ) {
      return moment(val).format('YYYY')
    }
    if ( config === 5 ) {
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
  if ( type === 'timer' ) {
    if ( config === 0 ) {
      return moment(val).format('HH:mm')
    }
    if ( config === 1 ) {
      return moment(val).format('HH:mm:ss')
    }
  }
};
/**
 * 时间转化
 * @param val
 * @param config
 */
export const formatTime = (val, config) => {
  return dateTimeFormat(val, 'timer', config)
};
/**
 * 日期转化
 * @param val
 * @param config
 */
export const formatDate = (val, config) => {
  return dateTimeFormat(val, 'date', config)
};
/**
 * 正则校验手机号
 * @param val
 * @returns {*}
 */
export const checkPhone = ( val ) => {
  const regular = /^(0|86|17951)?(13[0-9]|15[012356789]|17[13678]|18[0-9]|14[57]|19[0-9]|16[0-9])[0-9]{8}$/;
  return regular.test(val)
};
export const checkguPhone = ( val ) => {
  const regular = /^(([0][1-9]{2,3})-)[0-9]{4,16}$/;
  return regular.test(val)
};
export const checkChinese = (val) => {
  const regular = /[\u4E00-\u9FA5]/g;
  return regular.test(val)
};
export const clearSpaces = (val) => {
  for ( let key in val) {
    if (typeof(val[key]) === 'string') {
      val[key] = val[key].replace(/\s*/g, "")
    }
  }
  return val
};
export const checkAccount = (val) => {
  const regular = /^[A-Za-z0-9!@#_-]{1,30}$/;
  return regular.test(val)
};
/**
 * 正则校验身份证号
 * @param val
 * @returns {*}
 */
export const checkIdcard = ( val ) => {
  const regular = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  return regular.test(val)
};
/**
 * 正则校验银行卡号
 * @param val
 * @returns {*}
 */
export const checkBanknum = ( val ) => {
  const regular = /^\d{16,19}$/;
  return regular.test(val)
};
/**
 * 正则校验开户行名称
 * @param val
 * @returns {*}
 */
export const chenkBankname = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z0-9]{1,20}$/;
  return regular.test(val)
};
/**
 * 正则校验统一社会信用代码
 * @param val
 * @returns {*}
 */
export const checkSocialcode = ( val ) => {
  const regular = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/;
  return regular.test(val)
};
/**
 * 正则校验企业名称，公司简称
 * @param val
 * @returns {*}
 */
export const checkenterprisename = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z0-9]{1,50}$/;
  return regular.test(val)
};
/**
 * 正则校验详细地址
 * @param val
 * @returns {*}
 */
export const checkdetailaddress = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z0-9-.#]{1,50}$/;
  return regular.test(val)
};
/**
 * 正则校验经营范围
 * @param val
 * @returns {*}
 */
export const chenkBusinessscope = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z0-9-.,]{1,500}$/;
  return regular.test(val)
};
/**
 * 正则校验详细地址
 * @param val
 * @returns {*}
 */
export const chenkExperience = ( val ) => {
  const regular = /^[\u4E00-\u9FA50-9]{1,4}$/;
  return regular.test(val)
};
/**
 * 正则校验法人姓名
 * @param val
 * @returns {*}
 */
export const chenkLegalname = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z·]{1,15}$/;
  return regular.test(val)
};
/**
 * 正则校验小区名称
 * @param val
 * @returns {*}
 */
export const checkvillageName = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-zⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ]{1,20}$/;
  return regular.test(val)
};
/**
 * 正则校验仪表名称
 * @param val
 * @returns {*}
 */
export const checkinsNameA = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z0-9]{1,10}$/;
  return regular.test(val)
};
/**
 * 正则校验表号/品牌型号
 * @param val
 * @returns {*}
 */
export const checkinsNumA = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z0-9]{1,30}$/;
  return regular.test(val)
};
/**
 * 正则校验表号/品牌型号
 * @param val
 * @returns {*}
 */
export const paystyle = ( val ) => {
  const regular = /^[\u4E00-\u9FA5A-Za-z1-9]{1,15}$/;
  return regular.test(val)
};
/**
 * 获取本周，本月，本季度时间
 */
export const obtainDataTime = (timeData,num) => {
  if ( timeData === 'thisWeekTime' ) {
    return getThisWeek()
  } else if ( timeData === 'LastWeekTime' ) {
    return getLastWeek()
  } else if ( timeData === 'thisMonthTime' ) {
    return getThisMonth()
  } else if( timeData === 'thisQuarter' ) {
    return getThisQuarter()
  }else if( timeData === "LastMonthTime"){
    return getLastMonth(num)
  }
};
/**
 * 获取本周时间
 */
export const getThisWeek = () => {
  let thisWeekTime = (() => {
    const startDate = new Date(moment().week(moment().week()).startOf('week').valueOf());   //这样是年月日的格式
    const endDate = new Date(moment().week(moment().week()).endOf('week').valueOf());
    return formDate( formatDate(startDate, 1), formatDate(endDate, 1) )
  })();
  return thisWeekTime
};
/**
 * 获取上周时间
 */
export const getLastWeek = () => {
  let LastWeekTime = (() => {
    const startDate = new Date(moment().week(moment().week() - 1).startOf('week').valueOf());
    const endDate = new Date(moment().week(moment().week() - 1).endOf('week').valueOf());
    return formDate( formatDate(startDate, 1), formatDate(endDate, 1) )
  })();
  return LastWeekTime
};
/**
 * 获取获取本月时间
 */
export const getThisMonth = () => {
  let ThisMonthTime = (() => {
    const startDate = new Date(moment().month(moment().month()).startOf('month').valueOf());
    const endDate = new Date(moment().month(moment().month()).endOf('month').valueOf());
    return formDate( formatDate(startDate, 1), formatDate(endDate, 1) )
  })();
  return ThisMonthTime
};
/**
 * 获取上月时间
 */
 export const getLastMonth = (num) => {
  let ThisMonthTime = (() => {
    const startDate = new Date(moment().month(moment().month()-1).startOf('month').valueOf());
    const endDate = new Date(moment().month(moment().month()-1).endOf('month').valueOf());
    return formDate( formatDate(startDate, num?num:1), formatDate(endDate, num?num:1) )
  })();
  return ThisMonthTime
};
/**
 *获取本季时间
 */
export const getThisQuarter = () => {
  let ThisQuarterTime = (() => {
    const startDate = new Date(moment().quarter(moment().quarter()).startOf('quarter').valueOf());
    const endDate = new Date(moment().quarter(moment().quarter()).endOf('quarter').valueOf());
    return formDate( formatDate(startDate, 1), formatDate(endDate, 1) )
  })();
  return ThisQuarterTime
};
/**
 * 本周、月等时间格式化
 */
export const formDate = ( firstTime, endTime ) => {
  let timerArr = [];
  timerArr.push(firstTime);
  timerArr.push(endTime);
  return timerArr
};
export const special  = (val) => {
  const regular =  /^[\u4e00-\u9fa5a-z0-9.]+$/gi;
  return regular.test(val)
};
export const specialDep  = (val) => {
  const regular =  /^[\u4e00-\u9fa5a-z.]+$/gi;
  return regular.test(val)
};
